package com.jiyun.vo.day07;

import com.jiyun.model.day07.Cataloges;

import java.util.ArrayList;
import java.util.List;

/**
 * Author yangYingJie
 * Date 2023/8/5  10:58
 */
public class CatalogIdes {

    public List<Cataloges> catalogList=new ArrayList<>();

    public CatalogIdes(List<Cataloges> catalogList){
        this.catalogList=catalogList;
    }

    //所有根节点
    public List<Cataloges> getCatalog(){
       List<Cataloges> rootCatalogList = new ArrayList<>();
        for (Cataloges catalog : catalogList) {
            if(0==catalog.getPid()){
                rootCatalogList.add(catalog);
            }
        }
        return rootCatalogList;
    }
    //根据根节点构造树
    public List<Cataloges> buildCatalog(){
        List<Cataloges> treeCatalog=new ArrayList<>();
        for (Cataloges treeRootCatalog : getCatalog()) {
            treeRootCatalog=buildChildCatalog(treeRootCatalog);
            treeCatalog.add(treeRootCatalog);
        }
        return treeCatalog;
    }

    //创建子树结构
    public Cataloges buildChildCatalog(Cataloges pNode){
        List<Cataloges> childCatalog=new ArrayList<>();
        for (Cataloges catalog1 : catalogList) {
            if(catalog1.getPid().toString().equals(pNode.getId().toString())){
                childCatalog.add(buildChildCatalog(catalog1));
            }
        }
        pNode.setChildren(childCatalog);
        return pNode;

    }

}
